﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Globalization;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Brain
{
    public static class BrainModelReader
    {
        public static BrainModel Read(Stream stream)
        {
            StreamReader reader = new StreamReader(stream);

            bool readVersion = false;
            float version = 0;

            int meshesCount = -1;

            BrainModel model = new BrainModel();

            bool ExportNormals = false;
            bool ExportTangentSpaceInfo = false;
            bool ExportSkinning = false;
            bool ExportAnimation = false;

            bool startedReadingVertices = false;
            int verticesToRead = -1;

            bool startedReadingIndices = false;
            int indicesToRead = -1;

            List<List<VertexPositionNormal>> vertices = new List<List<VertexPositionNormal>>();

            while (!reader.EndOfStream)
            {
                string line = reader.ReadLine();
                if (!readVersion)
                {
                    // this SHOULD be the first line
                    // of the system, we need to know
                    // what version the file was exporter
                    if (line.StartsWith("BMv"))
                    {
                        version = float.Parse(line.Split('v')[1], CultureInfo.InvariantCulture);
                        readVersion = true;
                    }
                }
                else
                {
                    if (meshesCount == -1)
                    {
                        // we need to find the meshes count
                        // this should be the next line
                        if (line.StartsWith("m:"))
                        {
                            meshesCount = int.Parse(line.Split(':')[1], CultureInfo.InvariantCulture);
                            model.Meshes = new BrainModelMesh[meshesCount];

                            for (int i = 0; i < meshesCount; i++)
                            {
                                vertices.Add(new List<VertexPositionNormal>());
                            }
                        }
                    }
                    else
                    {
                        // read options or vertices
                        if (line.StartsWith("vertex:"))
                        {
                            verticesToRead = int.Parse(line.Split(':')[1], CultureInfo.InvariantCulture);
                            startedReadingVertices = true;
                        }

                        if (startedReadingVertices)
                        {
                            if (line.StartsWith("v:"))
                            {
                                string[] vertex = line.Split(':');
                                float X = float.Parse(vertex[1], CultureInfo.InvariantCulture);
                                float Y = float.Parse(vertex[2], CultureInfo.InvariantCulture);
                                float Z = float.Parse(vertex[3], CultureInfo.InvariantCulture);
                                float TexX = float.Parse(vertex[4], CultureInfo.InvariantCulture);
                                float TexY = float.Parse(vertex[5], CultureInfo.InvariantCulture);

                                Vector3 pos = new Vector3(X, Y, Z);
                                Vector2 tex = new Vector2(TexX, TexY);

                                VertexPositionNormal v = new VertexPositionNormal(pos, Vector3.Up);
                                vertices[0].Add(v);
                            }
                        }

                        if (line.StartsWith("indices:"))
                        {
                        }
                    }
                }
            }

            reader.Dispose();

            for (int i = 0; i < model.Meshes.Length; i++)
            {
                BrainModelMesh mesh = model.Meshes[i];
                List<VertexPositionNormal> vert = vertices[i];

                mesh.VertexBuffer = new VertexBuffer(Engine.Instance.GraphicsDevice, typeof(VertexPositionNormal), vert.Count, BufferUsage.None);
                mesh.VertexBuffer.SetData(vert.ToArray());
            }

            return model;
        }
    }
}
